package com.bnz.servlet;

import com.alibaba.fastjson.JSON;
import com.bnz.pojo.Classes;
import com.bnz.service.ClassesServce;
import com.bnz.service.impl.ClassesServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/classes")
public class ClassesServlet extends HttpServlet {

    private ClassesServce classesServce;
    @Override
    public void init() throws ServletException {
        classesServce = new ClassesServiceImpl();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf8");
        // 1. 查询出所有的班级
        List<Classes> classesList = classesServce.findAll();
        // 2. 转换为字符串输出
        resp.getWriter().println(JSON.toJSONString(classesList));
        resp.getWriter().flush();
    }
}
